import json

import re
from django import http
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.mail import send_mail
from django.shortcuts import render
from django.views import View
from fdfs_client.client import Fdfs_client

from boxuegu import settings
from boxuegu.settings import MEDIA_URL
from courses.models import Course
from operation.models import UserCourse
from users.forms import UserInfoForm, UploadImageForm
from users.models import UserProfile

# 首页
from utils.views import LoginRequiredJSONMixin


class IndexView(View):
    def get(self, request):
        return render(request, 'index.html')


# 用户注册
class RegisterView(View):
    def get(self, request):
        return render(request, 'register.html')


# 用户登陆
class LoginView(View):
    def get(self, request):
        return render(request, 'login.html')


# 退出登陆
class LogoutView(View):
    pass


# 忘记密码
class ForgetPwdView(LoginRequiredJSONMixin, View):
    pass


# 修改密码
class ResetView(View):
    pass


# 获取当前登录用户信息并修改
class UserInfoView(LoginRequiredMixin, View):
    """用户中心"""

    def get(self, request):
        return render(request, 'usercenter-info.html', {'MEDIA_URL': MEDIA_URL})


class UserCreateView(LoginRequiredMixin, View):
    def post(self, request, ):
        username = request.user.username
        nick_name = request.POST.get('nick_name')
        birthday = request.POST.get('birthday')
        gender = request.POST.get('gender')
        address = request.POST.get('address')
        mobile = request.POST.get('mobile')

        if not all([nick_name, birthday, gender, address, mobile]):
            return http.JsonResponse({'code': 5006, "status": "error"})

        user = UserProfile.objects.filter(username=username).update(
            nick_name=nick_name,
            birthday=birthday,
            gender=gender,
            address=address,
            mobile=mobile,

        )
        return http.JsonResponse({

            "status": "success",
            'nick_name': user.nick_name,
            'birthday': user.birthday,
            'gender': user.gender,
            'address': user.address,
            'mobile': user.mobile,

        })


# 修改用户头像
class UploadImageView(LoginRequiredMixin, View):
    def post(self, request):
        image_form = UploadImageForm(request.POST, request.FILES, instance=request.user)
        if image_form.is_valid():
            image_form.save()

            return http.JsonResponse({"status": "success",
                                      "msg": '头像修改成功',

                                      })


# 我的课程
class MyCourseView(View):
    def get(self, request):
        user_id = request.user.id
        user_courses = UserCourse.objects.filter(user_id=user_id).all()

        return render(request, 'usercenter-mycourse.html', {
            'user_courses': user_courses,
            'MEDIA_URL': settings.MEDIA_URL,
        })


# 我的收藏
class MyFavOrgView(View):
    pass


# 授课教师
class MyFavTeacherView(View):
    pass


# 公开课程
class MyFavCourseView(View):
    pass  # 获取当前登录用户信息


# 我的消息
class MyMessageView(View):
    pass
